﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyNhaSach
{
    public partial class FormDangNhap : Form
    {
        private KetNoiCSDL ketnoi = new KetNoiCSDL();        
        private NguoiDung nguoidung = new NguoiDung();
        private SqlDataReader reader;
        public FormDangNhap()
        {
            InitializeComponent();
            //textBoxMatKhau.Focus();            
        }

        private void buttonDangNhap_Click(object sender, EventArgs e)
        {
            nguoidung.tenDangNhap=textBoxDangNhap.Text;
            nguoidung.MatKhau=textBoxMatKhau.Text;
            int kiemtra = KiemTra(nguoidung);
            if (kiemtra == 1)
            {
                label3.Text = "Đăng Nhập thành công - Admin - " + nguoidung.tenDangNhap;
                nguoidung.PhanQuyen = "admin";   
                //this.Close();
            }
            else if (kiemtra == 2)
            {
                label3.Text = "Đăng Nhập thành công - User -" + nguoidung.tenDangNhap;
                nguoidung.PhanQuyen = "user";
                //this.Close();
            }
            else
                label3.Text = "Tên Đăng Nhập Hoặc Mật Khẩu bị sai";
        }

        private void buttonXoaTrang_Click(object sender, EventArgs e)
        {
            textBoxDangNhap.Text = "";
            textBoxMatKhau.Text = "";
        }

        private int KiemTra(NguoiDung user)
        {
            String t1 = "SELECT * FROM NHANVIEN WHERE TAIKHOAN = '" + user.tenDangNhap + "'and MATKHAU = '" + user.MatKhau + "' and ADMIN = 'admin'";
            String t2 = "SELECT * FROM NHANVIEN WHERE TAIKHOAN = '" + user.tenDangNhap + "'and MATKHAU = '" + user.MatKhau + "'";
            reader = ketnoi.thihanhDataReader(t1);
            if (reader.Read())//neu nguoi dung la admin tra ve 1
                return 1;
            else
            {
                reader = ketnoi.thihanhDataReader(t2);
                if (reader.Read())//neu nguoi dung la user tra ve 2
                    return 2;
            }
            return 0;//neu nguoi dung khong hop le tra ve 0
        }

    }
}
